package com.qire.manhua.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.qire.common.constant.CacheStorageKey;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.support.base.BaseDialog;
import com.qire.common.utils.CacheStorageUtil;
import com.qire.manhua.R;
import com.qire.manhua.controller.ComicDownloadSelectorController;
import com.qire.manhua.databinding.DialogVipDownloadPromptBinding;

/**
 * 下载是的开通VIP获得优惠提示引导对话框 <br />
 * 会员下载时享有的免费特权 <br />
 * 用于批量下载缓存页，如果下载内容存在VIP免费章节在点击下载时弹出
 */
public class VipDownloadPromptDialog extends BaseDialog {

    private final boolean isFirst = CacheStorageUtil.queryBoolean(CacheStorageKey.COMIC_DOWNLOAD_UNLOCK_FIRST, true);

    private final DialogVipDownloadPromptBinding binding;

    private final float totalPrice;

    public VipDownloadPromptDialog(Context context, float totalPrice, ComicDownloadSelectorController controller) {
        super(context, R.style.FullDialog);
        this.totalPrice = totalPrice;

        binding = DialogVipDownloadPromptBinding.inflate(LayoutInflater.from(context));
        binding.setVipDownloadPromptDialog(this);
        binding.setComicDownloadSelectorController(controller);
    }

    private void setGravityBottom() {
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(binding.getRoot());
        setGravityBottom();
    }

    @Override
    public void show() {
        super.show();
        updateFirstShowState();
    }

    /**
     * 判断用户账户余额是否足够解锁本章节
     *
     * @return 金币或金豆存在一种足够解锁都返回false, 否则返回true
     */
    private boolean isInsufficientBalance() {
        if (UserEntity.self.getCoinBalance() >= totalPrice) {
            return false;
        }
        return true;
    }

    /**
     * 更新首次显示状态。引导层只在第一次显示，之后不在显示
     */
    public void updateFirstShowState() {
        if(isFirst) {
            CacheStorageUtil.save(CacheStorageKey.COMIC_DOWNLOAD_UNLOCK_FIRST, false);
        }
    }

    public int getGuideVisible() {
        return isFirst ? View.VISIBLE : View.GONE;
    }

    public CharSequence getVipFreeChaptersCountInfo() {
        StringBuilder builder = new StringBuilder();
        builder.append("1、付费作品付费解锁7折优惠；\n")
                .append("2、会员章节免费缓存；\n")
                .append("3、会员专区不定期更新会员免费阅读作品；\n")
                .append("4、会员每日可领取20金豆；\n")
                .append("5、会员专属头像以及可发送彩色弹幕；");
        return builder;
    }

    public CharSequence getBalanceTips() {
        return isInsufficientBalance() ? "余额不足" : "余额充足";
    }

    public int getBalanceTipsBgRid() {
        return isInsufficientBalance() ? R.drawable.app_comic_download_unlock_tips_buoy_red : R.drawable.app_comic_download_unlock_tips_buoy_green;
    }

}
